package com.ljm.boot.rocketmq.procduer;

import com.ljm.boot.rocketmq.domain.OrderInfo;
import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;
import java.util.Date;

public class OrderProducer {

    //@Autowired
    @Resource(name ="rocketMQTemplate" )
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private MyRocketMQTemplate myRocketMQTemplate;

    /**
     * 发送同步消息
     * @throws Exception
     */
    public void sendMessage() throws Exception{
        String topic = "helloBoot";
        Message msg = MessageBuilder.withPayload("springboot发送同步消息").build();
        rocketMQTemplate.send(topic,msg);
    }

    /**
     * 发送异步消息
     * @throws Exception
     */
    public void sendAsyncMessage() throws Exception {
        String topic = "helloBoot";
        Message msg = MessageBuilder.withPayload("springboot发送异步消息").build();
        rocketMQTemplate.asyncSend(topic,msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送状态:"+sendResult.getSendStatus());
            }

            @Override
            public void onException(Throwable throwable) {

            }
        });
    }

    /**
     * 发送单向消息
     * @throws Exception
     */
    public void sendOneWayMessage() throws Exception {
        String topic = "helloBoot";
        rocketMQTemplate.sendOneWay(topic, "springboot发送单向消息");
    }

    /**
     * 发送顺序消息
     * @throws Exception
     */
    public void sendOrderlyMessage() throws Exception {
        String topic = "helloBoot";
        for (int i = 0; i < 10; i++) {
            rocketMQTemplate.sendOneWayOrderly( topic,"springboot发送单向消息" + i, String.valueOf(i));
        }
    }

    /**
     * 发送延时消息
     * @throws Exception
     */
    public void sendDelayMessage() throws Exception {
        String topic = "helloBoot";
        Message msg = MessageBuilder.withPayload("springboot发送延时消息" + new Date()).build();
        // 发送超时时间 3000(3秒钟)  rocketmq超时等级3
        rocketMQTemplate.syncSend(topic, msg, 3000, 3);
    }

    /**
     * 发送tag消息过滤
     * todo tag只能跟topic写在一起用:分割
     * @throws Exception
     */
    public void sendTagFilterMessage() throws Exception {
        Message msg1 = MessageBuilder.withPayload("消息A").build();
        // todo 它的tag标签只能写在topic中用:分割, :左边是主题, :右边是tag, 所以我们在命令主题的时候不要带:
        rocketMQTemplate.sendOneWay("tagFilterBoot:TagA", msg1);
        Message msg2 = MessageBuilder.withPayload("消息B").build();
        rocketMQTemplate.sendOneWay("tagFilterBoot:TagB", msg2);
        Message msg3 = MessageBuilder.withPayload("消息C").build();
        rocketMQTemplate.sendOneWay("tagFilterBoot:TagC", msg3);
    }

    /**
     * 发送sql92消息过滤
     * todo sql过滤属性只能写在setHeader里面, message没有对应的put
     * @throws Exception
     * 在broker.conf配置添加如下配置, 开始SQL92过滤
     * enablePropertyFilter = true
     */
    public void sendSql92FilterMessage() throws Exception {
        Message msg1 = MessageBuilder.withPayload("美女A, 年龄22, 体重45")
                .setHeader("age", 22)
                .setHeader("weight", 45)
                .build();

        rocketMQTemplate.sendOneWay("SQL92FilterBoot", msg1);
        Message msg2 = MessageBuilder.withPayload("美女B, 年龄25, 体重60")
                .setHeader("age", 25)
                .setHeader("weight", 60)
                .build();
        rocketMQTemplate.sendOneWay("SQL92FilterBoot", msg2);
        Message msg3 = MessageBuilder.withPayload("美女C, 年龄40, 体重70")
                .setHeader("age", 40)
                .setHeader("weight", 70)
                .build();
        rocketMQTemplate.sendOneWay("SQL92FilterBoot", msg3);
    }

    /**
     * 发送事务消息
     */
    public String sendTransaction(String orderNo) {
        System.out.println("发送消息");
        OrderInfo orderInfo = new OrderInfo();
        OrderInfo vo = new OrderInfo();

        // setHeader可以额外添加值
        Message<OrderInfo> message = MessageBuilder.withPayload(vo).setHeader("orderNo",orderNo).build();
        //发送事务消息
        String topic ="helloBoot";
        TransactionSendResult result = rocketMQTemplate.sendMessageInTransaction(topic, message, vo);
        System.out.println("发送的状态:"+result.getSendStatus());
        System.out.println("本地事务执行结果:"+ result.getLocalTransactionState());
        int i = 1/0;
        if(LocalTransactionState.COMMIT_MESSAGE.equals(result.getLocalTransactionState())){
            return "退款成功";
        }else{
            return "退款失败";
        }
    }
}
